<?php
/**
 * Created by PhpStorm.
 * User: giozola
 * Date: 2018/6/6
 * Time: 15:34
 */

namespace addons\hlapp\dto;


class Result
{
    /**
     * $code
     * 1 正常  0失败
     */
    public $code = 1;
    public $result;
    public $message;

    function __construct($c,$m,$r){
        $this->code = $c;
        $this->message = $m;
        $this->result = $r;
    }

    static public function getOk($r=null){
        $result = new Result(1,"成功",$r);
        return json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    static public function getError($m="失败"){
        $result = new Result(0,$m,null);
        return json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    /**
     * 返回成功状态的对象
     * @param null $r
     * @return Result
     */
    static public function getResultOk($r=null){
        return  new Result(1,"成功",$r);
    }

    /**
     * 返回失败状态的对象
     * @param string $m
     * @return Result
     */
    static public function getResultError($m="失败"){
        return  new Result(0,$m,null);
    }

    /**
     * 当前对象返回json数据格式
     * @return false|string
     */
    public function toJson(){
        return json_encode($this,JSON_UNESCAPED_UNICODE);
    }

    public static function objestToJson($result){
        return json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    public function setMessage($m){
         $this->message = $m;
    }

    public function setResult($r){
        $this->result = $r;
    }

    public function setCode($c){
        $this->code = $c;
    }

}